package com.emuporium.gymrat.server.exercise;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.ExerciseDB;

@SuppressWarnings("serial")
public class ExerciseEquipmentCreateSubmitServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("name");
		String shortName = request.getParameter("shortName");
		String redir = request.getParameter("redir");

		int userId;

		if (request.getParameter("public") != null)
			userId = 0;
		else
			userId = new Integer(request.getParameter("userId"));

		ExerciseDB.insertEquipment(name, shortName, userId);

		if (!redir.equals(""))
			request.setAttribute("redirecturl", redir);
		else
			request.setAttribute("redirecturl", "/exercise/create");
		String url = "/includes/redirect.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

}
